﻿namespace Yz.Core.Tools
{
    /// <summary>
    /// 生成懒加载单例模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class YzSingleton<T> where T : class
    {
        // Initializes a new instance of the Lazy<T> class. When lazy initialization occurs, the specified
        //     initialization function and initialization mode are used.
        private static readonly Lazy<T> current = new Lazy<T>(
            () =>
                Activator.CreateInstance<T>(),    // factory method
                true                              // true 表示此示例可由多个线程同时使用；false 表示此实例一次只能由一个线程使用。
        );
        /// <summary>
        /// 懒加载的实例
        /// </summary>
        public static T Current => current.Value;
    }
}
